package org.compression.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.compression.mapper.BookMagamentMapper;
import org.compression.services.imp.BookMagamentServiceImp;
import org.model.business.AddModel;
import org.model.business.BookMagament;
import org.model.comm.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.spring.web.json.Json;

@RestController
@RequestMapping(value = "/book")
public class BookMagamentController {
	private Logger logger = LoggerFactory.getLogger(BookMagamentController.class);

	@Autowired
	BookMagamentServiceImp mBookMagamentServiceImp;

	@Autowired
	BookMagamentMapper mBookMagamentMapper;
	
	@Value("${file.path}")
	public String filepath;

	@Value("${file.pathImage}")
	public String pathImage;


	@RequestMapping(value ="/getMember", method = RequestMethod.POST)
	public Object getMember(@RequestBody Page mPage) {
		return mBookMagamentServiceImp.getMember(mPage);
	}

	@RequestMapping(value ="/selectName", method = RequestMethod.POST)
	public Object selectName(@RequestBody BookMagament mBookMagament) {
		return mBookMagamentServiceImp.selectName(mBookMagament);
	}
	
	//新增书籍
	@RequestMapping(value ="/addbook", method = RequestMethod.POST)
	public Object addbook(@RequestBody AddModel mAddModel) {
		//System.out.println(mAddModel.getmBookMagament().getCreatedate());
		mBookMagamentServiceImp.insertBook(mAddModel);
		 return mBookMagamentServiceImp.getMember(mAddModel.getmPage());
	}
	
	@RequestMapping(value ="/insertBook/{createdate}", method = RequestMethod.POST,produces="application/json; utf-8")
	public Object insertBook(@RequestParam("file") MultipartFile file,@PathVariable String  createdate) {
	//	BookMagament mBookMagament=(BookMagament)JSONObject.parseObject(data, BookMagament.class);
		
		
	//	mBookMagamentMapper.insertBookMagament(mBookMagament);
	//	System.out.println(mBookMagament.getId());
		
	//  mBookMagament.setPath(file.getOriginalFilename());
		
		return mBookMagamentServiceImp.insertBook(file,createdate);
		
		
		
	/*	System.out.println(createdate);
		if (!file.isEmpty()) {
			try {
				BufferedOutputStream out = new BufferedOutputStream(
						new FileOutputStream(new File(filepath+file.getOriginalFilename())));
				out.write(file.getBytes());
				out.flush();
				out.close();
		//	mBookMagamentServiceImp.insertBook(mBookMagament);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			} catch (IOException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			}
			return "上传成功";
		} else {
			return "上传失败，因为文件是空的.";
		}
		*/
	
		/*mBookMagament.setPath(file.getOriginalFilename());
		if (!file.isEmpty()) {
			try {
				BufferedOutputStream out = new BufferedOutputStream(
						new FileOutputStream(new File(filepath+file.getOriginalFilename())));
				out.write(file.getBytes());
				out.flush();
				out.close();
				mBookMagamentServiceImp.insertBook(mBookMagament);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			} catch (IOException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			}
			return "上传成功";
		} else {
			return "上传失败，因为文件是空的.";
		}*/
	}


	@RequestMapping(value ="/insertBookCover", method = RequestMethod.POST)
	public Object insertBookCover(@RequestParam("data") String  data,@RequestParam("file") MultipartFile file) {
	
		
		System.out.println(data);
		
		return mBookMagamentServiceImp.selectCreateTime(data);
		
		
		
		
		
	/*	
		BookMagament mBookMagament=(BookMagament)JSONObject.parseObject(data, BookMagament.class);
		mBookMagament.setPath(file.getOriginalFilename());
		if (!file.isEmpty()) {
			try {
				BufferedOutputStream out = new BufferedOutputStream(
						new FileOutputStream(new File(filepath+file.getOriginalFilename())));
				out.write(file.getBytes());
				out.flush();
				out.close();
				mBookMagamentServiceImp.insertBook(mBookMagament);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			} catch (IOException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			}
			return "上传成功";
		} else {
			return "上传失败，因为文件是空的.";
		}*/
	}













	/**
	 * 文件上传具体实现方法（单文件上传）
	 *
	 * @param file
	 * @return
	 * 
	 * @author 单红宇(CSDN CATOOP)
	 * @create 2017年3月11日
	 */
	/*    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file,BookMagament  mBookMagament) {
        if (!file.isEmpty()) {
            try {
                // 这里只是简单例子，文件直接输出到项目路径下。
                // 实际项目中，文件需要输出到指定位置，需要在增加代码处理。
                // 还有关于文件格式限制、文件大小限制，详见：中配置。
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(file.getOriginalFilename())));
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }
            return "上传成功";
        } else {
            return "上传失败，因为文件是空的.";
        }
    }

	 *//**
	 * 多文件上传 主要是使用了MultipartHttpServletRequest和MultipartFile
	 *
	 * @param request
	 * @return
	 * 
	 * @author 单红宇(CSDN CATOOP)
	 * @create 2017年3月11日
	 *//*
    @RequestMapping(value = "/upload/batch", method = RequestMethod.POST)
    public @ResponseBody String batchUpload(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        MultipartFile file = null;
        BufferedOutputStream stream = null;
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    stream = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
                    stream.write(bytes);
                    stream.close();
                } catch (Exception e) {
                    stream = null;
                    return "You failed to upload " + i + " => " + e.getMessage();
                }
            } else {
                return "You failed to upload " + i + " because the file was empty.";
            }
        }
        return "upload successful";
    } */


}
